package com.abel.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.domain.Education;
import com.abel.web.view.EducationInfoViewBean;

public class EducationInfoViewValidator implements Validator{

		@SuppressWarnings("rawtypes")
		@Override
		public boolean supports(Class clazz) {
			return EducationInfoViewBean.class.equals(clazz);
		}

		@Override
		public void validate(Object obj, Errors errors) {

			EducationInfoViewBean viewBean = (EducationInfoViewBean)obj;
			
			if (viewBean.getEditId()==0){
				if (viewBean.getEduType() == null
						|| viewBean.getEduType().equals("")) {
					errors.rejectValue("eduType", "error.null",null,"Education Level is required field");
				}
				
				if (viewBean.getEduType() == null
						|| (!viewBean.getEduGraduateYn().equals("Y") && 
						!viewBean.getEduGraduateYn().equals("N"))) {
					errors.rejectValue("eduGraduateYn", "error.null",null,"Invalid Graduate Y/N");
				}
				
				if (viewBean.getEduDegree() == null
						|| viewBean.getEduDegree().equals("")) {
					errors.rejectValue("eduDegree", "error.null",null,"Degree is a required field");
				}
	
				if (viewBean.getEduGraduateMajor() == null
						|| viewBean.getEduGraduateMajor().equals("")) {
					errors.rejectValue("eduGraduateMajor", "error.null",null,"Major is a required field");
				}
				
				if (viewBean.getEduSchoolName() == null
						|| viewBean.getEduSchoolName().equals("")) {
					errors.rejectValue("eduSchoolName", "error.null",null,"School Name is a required field");
				}
	
				if (viewBean.getEduSchoolCity() == null
						|| viewBean.getEduSchoolCity().equals("")) {
					errors.rejectValue("eduSchoolCity", "error.null",null,"School City is a required field");
				}
				
				if (viewBean.getEduSchoolState() == null
						|| viewBean.getEduSchoolState().equals("")) {
					errors.rejectValue("eduSchoolState", "error.null",null,"School State is a required field");
				}
	
//				if (viewBean.getEduGraduateYear() == null
//						|| viewBean.getEduGraduateYear().equals("")) {
//					errors.rejectValue("eduGraduateYear", "error.null",null,"Graduate Year is a required field");
				if (viewBean.getEduGraduateYear() != null 
					&& !viewBean.getEduGraduateYear().equals("") 
					&& !viewBean.getEduGraduateYear().matches("[0-9]\\d{3}")) {
				errors.rejectValue("eduGraduateYear", "error.null",null,"Invalid Graduation Year");
				}
			}
			else
			{
				for(Education ed : viewBean.getEduList()) 
				{
					if (viewBean.getEditId() == ed.getEduInfoId())
					{
						if (ed.getEduType() == null
								|| ed.getEduType().equals("")) {
							errors.rejectValue("eduType", "error.null",null,"Education Level is required field");
						}
						
						if (ed.getEduType() == null
								|| (!ed.getEduGraduateYn().equals("Y") && 
								!ed.getEduGraduateYn().equals("N"))) {
							errors.rejectValue("eduGraduateYn", "error.null",null,"Invalid Graduate Y/N");
						}
						
						if (ed.getEduDegree() == null
								|| ed.getEduDegree().equals("")) {
							errors.rejectValue("eduDegree", "error.null",null,"Degree is a required field");
						}
			
						if (ed.getEduGraduateMajor() == null
								|| ed.getEduGraduateMajor().equals("")) {
							errors.rejectValue("eduGraduateMajor", "error.null",null,"Major is a required field");
						}
						
						if (ed.getEduSchoolName() == null
								|| ed.getEduSchoolName().equals("")) {
							errors.rejectValue("eduSchoolName", "error.null",null,"School Name is a required field");
						}
			
						if (ed.getEduSchoolCity() == null
								|| ed.getEduSchoolCity().equals("")) {
							errors.rejectValue("eduSchoolCity", "error.null",null,"School City is a required field");
						}
						
						if (ed.getEduSchoolState() == null
								|| ed.getEduSchoolState().equals("")) {
							errors.rejectValue("eduSchoolState", "error.null",null,"School State is a required field");
						}
			
//						if (ed.getEduGraduateYear() == null
//								|| ed.getEduGraduateYear().equals("")) {
//							errors.rejectValue("eduGraduateYear", "error.null",null,"Graduate Year is a required field");
//						}
						if (ed.getEduGraduateYear() != null 
							&& !ed.getEduGraduateYear().equals("") 
							&& !ed.getEduGraduateYear().matches("[0-9]\\d{3}")) {
						errors.rejectValue("eduGraduateYear", "error.null",null,"Invalid Graduation Year");
						}
					}
				}
			}
	}
}
